//////////////////////////////////////////////////
// STUDENT RESEARCH PROJECT						//
// UNIVERSITY OF HAGEN 2012						//
// File:	StatisticWIN32.cpp					//
// Date:	01.09.2012							//
// Author:	Max Pejs							//
//////////////////////////////////////////////////

#include "Statistic.h"

UInt16 Statistic::corrFramesCNT		= 0;
UInt16 Statistic::invalidFramesCNT	= 0;
bool Statistic::IsActivated			= 0;

unsigned long Statistic::lastTimerStoreStat = 0;
extern unsigned long millis();

void Statistic::ResetStatistics(void)
{
	printf("[INFO] Reset statistic");

	// reset counters
	corrFramesCNT = 0;
	invalidFramesCNT = 0;
}

void Statistic::StoreStatistic()
{
	if(!IsActivated)
	{
		return;
	}
}

void Statistic::SendStatistic()
{
	if(!IsActivated)
	{
		sendStatisticAreDeactivated();
		return;
	}

	printf("[INFO] Correct: ");	printf("%i\n",corrFramesCNT);
	printf(" Invalid: ");	printf("%i\n",invalidFramesCNT);
	printf(" Quality: ");

	UInt8 nmb = 0;
	UInt16 frameSum = corrFramesCNT + invalidFramesCNT;

	// avoid null division
	if(frameSum != 0)
	{
		nmb = (UInt8)(( (UInt32)corrFramesCNT * 100) / (corrFramesCNT + invalidFramesCNT));
	}
	printf("%i",nmb);	printf("%\n");
}

void Statistic::FrameReceived(bool valid)
{
	if(!IsActivated)
	{
		return;
	}

	if(valid)
	{
		corrFramesCNT++;
	}
	else
	{
		invalidFramesCNT++;
	}
}

void Statistic::sendStatisticAreDeactivated(void)
{
	printf("Statistics are deactivated. For activating type \"staton\"\n");
}

void Statistic::SendStatus()
{
	printf("[INFO] Statistic are ");
	
	if(IsActivated)
	{
		printf("on\n");
	}
	else
	{
		printf("off\n");
	}
}

void Statistic::SetState(bool state)
{
	IsActivated = state;
	STATISTIC.SendStatus();
}